<?php

class UserController extends Controller {

    public function actions() {
       return array(
            'captcha' => array(
                'class'     => 'CCaptchaAction',
                'backColor' => 0x563D7C,
                'foreColor' => 0x55FF00,
                'width'     => 110,
                'minLength' => 4,
                'maxLength' => 4,
            )
        );
    }

    public $layout = '//layouts/column2';

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function accessRules() {
        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('index', 'view'),
                'users' => array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array('create', 'update', 'captcha', 'upload', 'view'),
                'users' => array('@'),
            ),
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
                'actions' => array('admin', 'delete', 'captcha', 'upload', 'update'),
                'users' => array('admin'),
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionView($id) {
        $this->render('view', array(
            'model' => $this->loadModel($id),
        ));
    }

    public function actionCreate() {
        $model = new User;
        if (isset($_POST['User'])) {
            $model->attributes = $_POST['User'];
            Yii::import('application.extensions.image.Image');
            if ($_POST['User']['avatar_large']) {
                $sourceFile = Yii::app()->basePath . '/..' . $_POST['User']['avatar_large'];
            } else {
                $sourceFile = Yii::app()->basePath . '/../uploads/avatar/avatar.jpg';
                $model->avatar_large = '/uploads/avatar/avatar.jpg';
            }
            $tem_time = date("YmdHis", time());
            $medium_img = new Image($sourceFile);
            $medium_img->resize(50, 50)->quality(100);
            $avatar_medium = $tem_time . '_avatar_medium_.' . substr($sourceFile, -3, 3);
            $medium_img->save('uploads/avatar/50/' . $avatar_medium);
            $model->avatar_medium = '/uploads/avatar/50/' . $avatar_medium;

            $small_img = clone $medium_img;
            $small_img->resize(30, 30)->quality(100);
            $avatar_small = $tem_time . '_avatar_small_.' . substr($sourceFile, -3, 3);
            $small_img->save('uploads/avatar/30/' . $avatar_small);
            $model->avatar_small = '/uploads/avatar/30/' . $avatar_small;

            if ($model->save()) {
                $this->redirect(array('view', 'id' => $model->id));
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    public function actionUpdate($id) {
        $model = $this->loadModel($id);

        if (isset($_POST['User'])) {
            $model->attributes = $_POST['User'];

            Yii::import('application.extensions.image.Image');
            if ($_POST['User']['avatar_large']) {
                $sourceFile = Yii::app()->basePath . '/..' . $_POST['User']['avatar_large'];
                $tem_time = date("YmdHis", time());

                @unlink(Yii::app()->basePath . '/..' . $model->avatar_medium);
                @unlink(Yii::app()->basePath . '/..' . $model->avatar_small);

                $medium_img = new Image($sourceFile);
                $medium_img->resize(50, 50)->quality(100);
                $avatar_medium = $tem_time . '_avatar_medium.' . substr($sourceFile, -3, 3);
                $medium_img->save('uploads/avatar/50/' . $avatar_medium);
                $model->avatar_medium = '/uploads/avatar/50/' . $avatar_medium;

                $small_img = clone $medium_img;
                $small_img->resize(30, 30)->quality(100);
                $avatar_small = $tem_time . '_avatar_small.' . substr($sourceFile, -3, 3);
                $small_img->save('uploads/avatar/30/' . $avatar_small);
                $model->avatar_small = '/uploads/avatar/30/' . $avatar_small;
            } else {
                if (!$model->avatar_large) {
                    $model->avatar_large = '/uploads/avatar/avatar.jpg';
                }
            }
            if ($model->save()) {
                $this->redirect(array('index'));
            }
        }
        $this->render('update', array('model' => $model,));
    }

    public function actionDelete($id) {
        if (Yii::app()->request->isPostRequest) {
            $this->loadModel($id)->delete();
            if (!isset($_GET['ajax']))
                $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
        }
        else
            throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
    }

    public function actionIndex() {
        $dataProvider = new CActiveDataProvider('User');
        $this->render('index', array(
            'dataProvider' => $dataProvider,
        ));
    }

    public function actionAdmin() {
        $model = new User('search');
        $model->unsetAttributes();
        if (isset($_GET['User']))
            $model->attributes = $_GET['User'];

        $this->render('admin', array(
            'model' => $model,
        ));
    }

    public function loadModel($id) {
        $model = User::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'user-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

    public function actionUpload() {
        Yii::import('application.extensions.image.Image');
        $file = CUploadedFile::getInstanceByName('file');
        $file->saveAs('uploads/tem/' . $file->getName());
        $sourceFile = Yii::app()->basePath . '/../uploads/tem/' . $file->getName();
        $image = new Image($sourceFile);
        $image->resize(180, 180)->quality(100);
        $thumbname = date("YmdHis", time()) . '_avatar_large.' . substr($sourceFile, -3, 3);
        $image->save('uploads/avatar/180/' . $thumbname);
        echo Yii::app()->baseUrl . '/uploads/avatar/180/' . $thumbname;
        @unlink($sourceFile);
    }

}
